﻿/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "Ucop/ErrorHandler.h"
#include <cstdio>
#include <cstdarg>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <strsafe.h>
#include <DbgHelp.h>
#include "Ucop/Util/String.h"
#include "Shared/Log/Log.h"
#include "Realmd/Global.h"
using namespace Ucop;


namespace Ucop {

    void ErrorHandler::HandleException(Exception &e)
    {
        printf("ErrorHandler::HandleException(Exception) %s\n", e.what());
    }

    void ErrorHandler::HandleException(LogicException &e)
    {
        printf("ErrorHandler::HandleException(LogicException) %s, %s\n", e.name(), e.what());
    }

    void ErrorHandler::HandleException(RuntimeException &e)
    {
        printf("ErrorHandler::HandleException(RuntimeException) %s, %s\n", e.name(), e.what());
    }

    void ErrorHandler::HandleException(std::exception &e)
    {
        printf("ErrorHandler::HandleException(std::exception) %s\n", e.what());
    }

    void ErrorHandler::HandleSystemException(void)
    {
        printf("ErrorHandler::HandleSystemException(void)\n");
    }

}

LONG WINAPI __UnhandledExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo)
{
    /* 构造dump生成文件 */
    TCHAR szDumpPath[MAX_PATH], szDumpFile[MAX_PATH];
    GetCurrentDirectory(MAX_PATH, szDumpPath);
    StringCbPrintf(szDumpFile, MAX_PATH, TEXT("%s\\Realmd.dmp"), szDumpPath);

    /* 创建文件 */
    HANDLE hFile = CreateFile(szDumpFile,
        GENERIC_WRITE,
        0,
        NULL,
        CREATE_ALWAYS,
        0,
        NULL);

    /* 创建异常信息结构体 */
    _MINIDUMP_EXCEPTION_INFORMATION   ExInfo;
    ExInfo.ThreadId = GetCurrentThreadId();
    ExInfo.ExceptionPointers = ExceptionInfo;
    ExInfo.ClientPointers = NULL;

    /* 输出dump文件 */
    MiniDumpWriteDump(GetCurrentProcess(),
        GetCurrentProcessId(),
        hFile,
        MiniDumpWithFullMemory,
        &ExInfo,
        NULL,
        NULL );

    return EXCEPTION_EXECUTE_HANDLER;
}

void OnLoginDBError(int _Id, const char* _Format, ...)
{
    char _Msg[0x1000];
    std::va_list _Arg;
    va_start(_Arg, _Format);
    Util::vsnprintf(_Msg, sizeof(_Msg), _Format, _Arg);
    va_end(_Arg);

    sLog.outDbError(_Msg);
}

void OnCharaDBError(int _Id, const char* _Format, ...)
{
	char _Msg[0x1000];
	std::va_list _Arg;
	va_start(_Arg, _Format);
	Util::vsnprintf(_Msg, sizeof(_Msg), _Format, _Arg);
	va_end(_Arg);

	sLog.outDbError(_Msg);
}

void OnWorldDBError(int _Id, const char* _Format, ...)
{
	char _Msg[0x1000];
	std::va_list _Arg;
	va_start(_Arg, _Format);
	Util::vsnprintf(_Msg, sizeof(_Msg), _Format, _Arg);
	va_end(_Arg);

	sLog.outDbError(_Msg);
}